package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CityItem;
import com.jzg.jcptadmin.data.vo.TotalCountInfo;
import com.jzg.jcptadmin.data.vo.WorkInfo;
import com.jzg.jcptadmin.viewinterface.HomeActivityInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/11.
 */
public class HomePresenter extends BasePresenter<HomeActivityInterface> {
    private DataManager mDataManager;
    private Subscription mSubscription;
    private HomeActivityInterface mView;

    public HomePresenter(DataManager dataManager) {
        mDataManager = dataManager;
    }

    @Override
    public void attachView(HomeActivityInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null)
            mSubscription.unsubscribe();
    }

    /**
     * 加载消息条数
     */
    public void loadCount() {
        Map<String, String> params = mView.getTotalCountParams();
        if (!checkSubmitInfoWithNoDialog(params)) return;
        mSubscription = mDataManager.loadTotalCount(params).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<TotalCountInfo>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                if (e != null&&mView!=null) {
                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(TotalCountInfo totalCountInfo) {
                mView.success(totalCountInfo);
            }
        });
    }

    public  void getWork(){
        Map<String, String> params = mView.getWorkParams();
        if (!checkSubmitInfoWithNoDialog(params)) return;
        mSubscription = mDataManager.loadWokeInfo(params).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<WorkInfo>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                if (e != null) {
                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(WorkInfo totalCountInfo) {
                mView.successForwork(totalCountInfo);
            }
        });
    }

    public void getCityData(Map<String, String> params) {
        mSubscription = mDataManager.getCityData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<CityItem>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null && mView != null) {
                            mView.showError(e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(CityItem cityItem) {

                        mView.cityDataSuccess(cityItem);


                    }
                });
    }
}
